package com.uva.dbcs.web.beans.gestionDeVehiculos.utils;

import javax.ejb.EJB;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.uva.dbcs.gestionDeVehiculos.despliegue.GestorDeVehiculosLocal;
import com.uva.dbcs.gestionDeVehiculos.dominio.Modelo;
import com.uva.dbcs.utilidades.utils.Utils;

@FacesConverter(value = "modeloConverter", forClass = Modelo.class)
public class ModeloConverter implements Converter {

	@EJB
	GestorDeVehiculosLocal gestorDeVehiculosLocal;

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		System.out.println("get as object modelo");
		
		if (Utils.isNumeric(value)){
	
			
			return gestorDeVehiculosLocal.getModeloById(Integer.parseInt(value));
		}
		
		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		
		System.out.println("get as string modelo");
		
		if (value!=null && value instanceof Modelo ){
			
			return String.valueOf(((Modelo)value).getIdmodelo());
		}
		
		return null;
	}

}
